package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.HomeBodyItemBean;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3LoadingHolder;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.widget.X3MarqueeText;

import java.util.ArrayList;
import java.util.List;

public class X3ServiceListItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<HomeBodyItemBean.ListBean> mList;

    /**
     * 0 正常
     * 1 空白占位
     * 2 loading
     */
    private int mEmptyType;

    private String statusName;

    public X3ServiceListItemAdapter(Context mContext,String statusName) {
        this.mContext = mContext;
        this.statusName = statusName;
        this.mList = new ArrayList<>();
        mEmptyType = 2;
    }

    /**
     * 下拉刷新
     * @param mList
     */
    public void changeAdapter(List<HomeBodyItemBean.ListBean> mList) {
        this.mList = mList;
        mEmptyType = X3StringUtils.isListEmpty(mList) ? 1 : 0;
        notifyDataSetChanged();
    }

    /**
     * 上拉加载
     * @param mList
     */
    public void changeAddAdapter(List<HomeBodyItemBean.ListBean> mList) {
        this.mList.addAll(mList);
        mEmptyType = X3StringUtils.isListEmpty(this.mList) ? 1 : 0;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_home_item, parent, false));
            case 2:
                return new X3LoadingHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_home_loading_item, parent, false));
            default:
                return new HomeNewVpHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_service_list_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case 0:
                HomeNewVpHolder vpHolder = (HomeNewVpHolder) holder;

                RecyclerView.LayoutParams vpLayout = (RecyclerView.LayoutParams) vpHolder.itemView.getLayoutParams();
                if (position == mList.size() - 1) {
                    vpLayout.topMargin = X3ScreenUtils.dipToPx(12,mContext);
                    vpLayout.bottomMargin = X3ScreenUtils.dipToPx(20,mContext);
                }else {
                    vpLayout.topMargin = X3ScreenUtils.dipToPx(12,mContext);
                    vpLayout.bottomMargin = 0;
                }
                holder.itemView.setLayoutParams(vpLayout);

                String receiveBaseKey = mList.get(position).receiveBaseKey;
                vpHolder.tv_order_id.setText("#" + ((!X3StringUtils.isEmpty(receiveBaseKey) && receiveBaseKey.length() >= 4) ? receiveBaseKey.substring(receiveBaseKey.length() - 4) : ""));

                List<String> serviceNameList = mList.get(position).serviceNameList;
                if (!X3StringUtils.isListEmpty(serviceNameList)) {
                    if (serviceNameList.size() == 1) {
                        vpHolder.tv_service_name.setText("#" + serviceNameList.get(0));
                    }else {
                        vpHolder.tv_service_name.setText(String.format(mContext.getString(R.string.service_name_more),"#" + serviceNameList.get(0),serviceNameList.size()));
                    }

                }else {
                    vpHolder.tv_service_name.setText(mContext.getString(R.string.x3_no_create_service));
                }

                X3GlideNewUtils.loadNormalImage(mContext, X3StringUtils.getImageUrl(mList.get(position).carBrandResourceUrl),vpHolder.iv_car,R.mipmap.x3_car_icon_no);

                String brandName = X3StringUtils.isEmpty(mList.get(position).carBrandName) ? "－" : mList.get(position).carBrandName;

                String seriesName = X3StringUtils.isEmpty(mList.get(position).carStyleName) ? "" : mList.get(position).carStyleName;

                vpHolder.tv_car.setText(brandName + " " + seriesName);

                vpHolder.tv_car_num.setText(mList.get(position).carNum + "");


                String mgtCreate = mList.get(position).mgtCreate;

                vpHolder.tv_time.setText(mContext.getString(R.string.x3_arrive_store_time) + ": " + X3DateUtils.HomeRelativeTime(mgtCreate,mContext));

                vpHolder.itemView.setOnClickListener(v -> {
                    if (iOnClickHomeWork != null) {
                        iOnClickHomeWork.iOnClickWorkGetCar(mList.get(position).id);
                    }
                });
                break;
            case 1:
                EmptyHolder emptyHolder = (EmptyHolder) holder;
                emptyHolder.tv_empty.setText(String.format(mContext.getString(R.string.x3_no_status_order),statusName));

                emptyHolder.rl_create.setVisibility(View.GONE);
                emptyHolder.rl_quick.setVisibility(View.GONE);
                break;
            case 2:

                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mEmptyType;
    }


    @Override
    public int getItemCount() {
        return X3StringUtils.isListEmpty(mList) ? 1 : mList.size();
    }

    IOnClickHomeWork iOnClickHomeWork;

    public void setiOnClickHomeWork(IOnClickHomeWork iOnClickHomeWork) {
        this.iOnClickHomeWork = iOnClickHomeWork;
    }

    public interface IOnClickHomeWork {

        // 接车单
        void iOnClickWorkGetCar(long id);

    }

    class HomeNewVpHolder extends RecyclerView.ViewHolder {
        X3MarqueeText tv_order_id;
        TextView tv_service_name;
        RelativeLayout rl_car;
        ImageView iv_car;
        TextView tv_car;
        TextView tv_car_num;
        TextView v_stoke;
        TextView tv_time;

        public HomeNewVpHolder(@NonNull View itemView) {
            super(itemView);
            tv_order_id = itemView.findViewById(R.id.tv_order_id);
            tv_service_name = itemView.findViewById(R.id.tv_service_name);
            rl_car = itemView.findViewById(R.id.rl_car);
            tv_car_num = itemView.findViewById(R.id.tv_car_num);
            iv_car = itemView.findViewById(R.id.iv_car);
            tv_car = itemView.findViewById(R.id.tv_car);
            v_stoke = itemView.findViewById(R.id.v_stoke);
            tv_time = itemView.findViewById(R.id.tv_time);

        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;
        RelativeLayout rl_create;
        RelativeLayout rl_quick;
        TextView tv_quick;

        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
            rl_create = itemView.findViewById(R.id.rl_create);
            rl_quick = itemView.findViewById(R.id.rl_quick);
            tv_quick = itemView.findViewById(R.id.tv_quick);
        }
    }
}
